package com.xinbochuang.machine.admin.controller.system;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinbochuang.machine.admin.domain.system.SysStorehouse;
import com.xinbochuang.machine.admin.dto.AesRequestBean;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.utils.AESUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 黄晓鹏
 * @date 2020-09-30
 */
@RestController
@RequestMapping("/system/storehouse")
public class SysStorehouseController extends BaseController {

    @Resource
    private ISysStorehouseService sysStorehouseService;

    /**
     * 根据计划编号获取详细信息
     */
    @GetMapping(value = "/getInPutDetailByQrCode")
    public AjaxResult getInPutDetailByQrCode(@RequestParam("qrcode") String qrcode) {
        SysStorehouse sysStorehouse = sysStorehouseService.getInPutDetailByQrCode(qrcode);
        if (sysStorehouse == null) {
            return AjaxResult.error();
        }
        return AjaxResult.data(sysStorehouse);
    }

    /**
     * 根据计划编号获取详细信息
     */
    @GetMapping(value = "/getInPutDetailByQrCodeN")
    public AjaxResult getInPutDetailByQrCodeN(@RequestParam("qrcode") String qrcode) {
        SysStorehouse sysStorehouse = sysStorehouseService.getInPutDetailByQrCode(qrcode);
        if (sysStorehouse == null) {
            return AjaxResult.error();
        }
        return AjaxResult.data(AESUtils.encode(JSON.toJSONStringWithDateFormat(sysStorehouse,"yyyy-MM-dd HH:mm:ss")));
    }
    /**
     * 根据计划编号获取详细信息
     */
    @GetMapping(value = "/input")
    public AjaxResult input(@RequestParam("id") String id,@RequestParam("storageRackId") String storageRackId,@RequestParam("storageRackPosition") String storageRackPosition) {
        sysStorehouseService.input(id,storageRackId,storageRackPosition);
        return AjaxResult.data("");
    }

    /**
     * 根据计划编号获取详细信息
     */
    @PostMapping(value = "/inputN")
    public AjaxResult input(@RequestBody AesRequestBean aesRequestBean) {
        String data=aesRequestBean.getData();
        data=AESUtils.decode(data);
        SysStorehouse sysStorehouse=JSON.parseObject(data,SysStorehouse.class);
        sysStorehouseService.input(sysStorehouse.getId()+"",sysStorehouse.getStorageRackId(),sysStorehouse.getStorageRackPosition());
        return AjaxResult.data("");
    }
    /**
     * 根据入库id查询一级清单列表
     *
     * @param inId 入库id
     * @return 一级清单列表
     */
    @GetMapping("/getByInId")
    public AjaxResult getByInId(Integer inId) {
        return AjaxResult.data(sysStorehouseService.list(Wrappers.<SysStorehouse>lambdaQuery().eq(SysStorehouse::getInId, inId)));
    }

}

